using System;
using System.Configuration;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using ScenarioFramework.WorkflowEngine.DataModel.WorkflowProcesses;
using WorkflowRelevantData = ScenarioFramework.WorkflowEngine.DataModel.WorkflowRelevantData;
using ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData;
using ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Entity;
using ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.Property;

using System.IO;
using System.Text;
using System.Reflection;
using ScenarioFramework.Modules.FormHelper;
using ScenarioFramework.Modules.DM.DataBase;
using ScenarioFramework.Modules.DM.Application;
using ScenarioFramework.Portal;


namespace ACEMPortal.ACEM.Infopath
{
	/// <summary>
	/// Summary description for InfoPathRedirect.
	/// </summary>
	public partial class InfoPathRedirect : System.Web.UI.Page
	{
		protected void Page_Load(object sender, System.EventArgs e)
		{
			string IdProcess = string.Empty;
			string IdActivity = string.Empty;
			FormHelper formHelper = Global.FormsHelper.GetFormHelper();

			//Retrieving IdProcess and IdActivity
			IdProcess = Request.QueryString["IdProcess"];
			IdActivity = Request.QueryString["IdActivity"];

			formHelper.LoadXmlAttributes(IdProcess,"",FormHelper.ACTION_PROCESSATTRIBUTES);


			/*
			string sIdDocument= formHelper.GetAttribute("IdDocument",FormHelper.ACTION_PROCESSATTRIBUTES);
			DocumentManagerDBHelper dmDBHelper = new DocumentManagerDBHelper();
			DocumentHelper docHelper=new DocumentHelper (dmDBHelper);
			
			
			string sIdFile=docHelper.GetInstanceFileName(sIdDocument,"1",IdActivity,"xml");
			string sFileName = docHelper.GetInstanceFileNameWithoutPath(sIdDocument , "1",IdActivity,"xml");
			string sFilePath = ConfigurationSettings.AppSettings["DownloadDirectory"];
			System.IO.File.Copy (sIdFile, sFilePath + sFileName, true);
			
			XmlDocument doc= new XmlDocument();
			doc.Load(sFilePath + sFileName);
			XmlNode Node= doc.SelectSingleNode("//IdDocumento");
			Node.InnerText=sIdDocument;
			XmlNode IdUser= doc.SelectSingleNode("//User");
			IdUser.InnerText=Page.User.Identity.Name;
			
			XmlNode Idproc= doc.SelectSingleNode("//idProcess"); 
			Idproc.InnerText=IdProcess;
			doc.Save(sFilePath + sFileName);
			*/

			string sUrl=string.Empty;
			string sMessage = formHelper.OnAction("Infopath." + IdActivity,IdProcess, IdActivity, "",  Page.User.Identity.Name, ref sUrl, true);

			XMLAction action = formHelper.GetAction("Infopath." + IdActivity);

			string sIdDocument= formHelper.GetAttribute(action.RedirectUrl,FormHelper.ACTION_PROCESSATTRIBUTES);
			DocumentManagerDBHelper dmDBHelper = new DocumentManagerDBHelper();
			DocumentHelper docHelper=new DocumentHelper (dmDBHelper);

			string sIdFile=docHelper.GetInstanceFileName(sIdDocument);
			string sFileName = docHelper.GetInstanceFileNameWithoutPath(sIdDocument);
			string sFilePath = ConfigurationSettings.AppSettings["DownloadDirectory"];
			System.IO.File.Copy (sIdFile, sFilePath + sFileName, true);

			string sRedirect =ConfigurationSettings.AppSettings["DMDownloadUrl"] + sFileName; 
			
			Response.Redirect(sRedirect);
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
		}
		#endregion
	}
}
